package multigest.vocappulary;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class VocTestActivity extends Activity {

	private static String TAG_WORDS = "words";
	private static String TAG_ORIGINAL = "original";
	private static String TAG_TRANSLATED = "translated";
	
	private String catalogfile = null;
	private int word_cnt = 0;
	private boolean words_right[];
	
	private JSONObject cata_json = null;
	private JSONArray words = null;
	
	private TextView word_DE = null;
	private EditText word_input = null;
	private Button btn_check = null;

		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_voc_test);
		
		catalogfile = getIntent().getStringExtra("catalog");
			 
		try {
			// Load JSON file
			cata_json = VocAppUtils.loadJSONFromAsset("Vocabulary/"+catalogfile, getAssets());	
			// Load words from JSON file
			words = cata_json.getJSONArray(TAG_WORDS);		
		} catch (JSONException e) {
           // AlertDialog instance
			Builder jsonErr = new AlertDialog.Builder(this);

           jsonErr.setTitle("JSON Error");
           jsonErr.setMessage("An error occurred while loading a vocabulary package!("+catalogfile+")");
           jsonErr.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
               	}
            });
           
            // Showing Alert Message
            jsonErr.show();			
            e.printStackTrace();
		}
		
		words_right = new boolean[words.length()];
				
		
		word_DE = (TextView) findViewById(R.id.word_test_DE);
		word_input = (EditText) findViewById(R.id.word_input);
		btn_check = (Button) findViewById(R.id.btn_test);
	
	}
	
	
	
	@Override
	protected void onStart(){
		super.onStart();

        try {
        	word_DE.setText(words.getJSONObject(word_cnt).getString(TAG_ORIGINAL));
    	} catch (JSONException e) {
			e.printStackTrace();
		}
				
        btn_check.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View view) {
				try {
					
					//Dont run for empty input
					if(word_input.getText().toString().equals("")){
						Toast.makeText(getApplicationContext(), "Error: No input!", Toast.LENGTH_LONG).show();
						return;
					}
					
					if(word_input.getText().toString().equals(words.getJSONObject(word_cnt).getString(TAG_TRANSLATED))){
						words_right[word_cnt] = true;
					}else{
						words_right[word_cnt] = false;
					}

					if(word_cnt +1 < words.length()){
						word_cnt++;
						word_DE.setText(words.getJSONObject(word_cnt).getString(TAG_ORIGINAL));
						word_input.setText("");
					}else{
						
						btn_check.setOnClickListener(null);
						
				    	Intent intent = new Intent(getApplicationContext(), VocResultActivity.class);
			           	intent.putExtra("new_result", VocAppUtils.getResult(words_right));
			           	intent.putExtra("catalog", catalogfile);
				    	startActivity(intent);
				    	
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
        });	
		

		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.voc_test, menu);
		return true;
	}

}
